package com.ch.mapdemo.util;

public class Demo {

	public static void main(String[] args) {
		Demo d = new Demo();
		Point1[] ps = new Point1[] { new Point1(114.309914,30.599556),//114.309914,30.599556
                new Point1(114.295688,30.592879),//114.295688,30.592879
                new Point1(114.292812,30.587726), //114.292812,30.587726
                new Point1(114.292812,30.587726), //114.292812,30.587726
                new Point1(114.30058,30.580318),//114.30058,30.580318
                new Point1(114.303606,30.586959),//114.303606,30.586959
                new Point1(114.304534,30.594751),//114.304534,30.594751
                new Point1(114.30838,30.590131),//114.30838,30.590131
                new Point1(114.308651,30.584182),//114.308651,30.584182
                new Point1(114.304495,30.584015),//114.304495,30.584015
                new Point1(114.301301,30.578759),//114.301301,30.578759
                new Point1(114.309437,30.578528),//114.309437,30.578528
                new Point1(114.323282,30.592786)};//114.323282,30.592786
		Point1 n1 = new Point1(114.303217,30.583553);  
        Point1 n2 = new Point1(114.307336,30.597592);  
        Point1 n3 = new Point1(114.286565,30.590056);  
        Point1 y1 = new Point1(114.227342,30.587987);  
        Point1 y2 = new Point1(120.1866 , 30.2672);  
        Point1 y4 = new Point1(120.1869 , 30.2718);  
        System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps));  
        System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps));  
        System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps));  
        System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps));  
        System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps));  
        System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps)); 

	}
	
	public static boolean isPtInPoly (double ALon , double ALat , Point1[] ps) {  
        int iSum, iCount, iIndex;  
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;  
        if (ps.length < 3) {  
            return false;  
        }  
        iSum = 0;  
        iCount = ps.length;  
        for (iIndex = 0; iIndex<iCount;iIndex++) {  
            if (iIndex == iCount - 1) {  
                dLon1 = ps[iIndex].getX();  
                dLat1 = ps[iIndex].getY();  
                dLon2 = ps[0].getX();  
                dLat2 = ps[0].getY();  
            } else {  
                dLon1 = ps[iIndex].getX();  
                dLat1 = ps[iIndex].getY();  
                dLon2 = ps[iIndex + 1].getX();  
                dLat2 = ps[iIndex + 1].getY();  
            }  
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上  
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {  
                if (Math.abs(dLat1 - dLat2) > 0) {  
                    //得到 A点向左射线与边的交点的x坐标：  
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);  
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：  
                    if (dLon < ALon) {  
                        iSum++;  
                    }  
                }  
            }  
        }  
        if ((iSum % 2) != 0) {  
            return true;  
        }  
        return false;  
    }  

}
